package com.oocl.aeat.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.oocl.aeat.dao.MerchantDao;
import com.oocl.aeat.dao.VerifyDao;
import com.oocl.aeat.dao.impl.MerchantDaoImpl;
import com.oocl.aeat.dao.impl.VerifyDaoImpl;
import com.oocl.aeat.dto.MerchantDto;
import com.oocl.aeat.pojo.Merchant;
import com.oocl.aeat.pojo.Verify;
import com.oocl.aeat.redis.MerchantCache;
import com.oocl.aeat.redis.VerifyCache;
import com.oocl.aeat.service.MerchantService;
import com.oocl.aeat.service.VerifyService;
import com.oocl.aeat.service.factory.MerchantServiceFactory;
import com.oocl.aeat.util.Const;

public class VerifyServiceImpl implements VerifyService {
	private VerifyDao verifyDao;
	private MerchantDao merchantDao;
	private MerchantService merchantService;

	public VerifyServiceImpl() {
		verifyDao = new VerifyDaoImpl();
		merchantDao = new MerchantDaoImpl();
		merchantService = MerchantServiceFactory.getInstance();
	}

	@Override
	public boolean addVerify(Verify verify) {
		// 校验Verify是否已经审核通过，如果已经通过。不存入DB，否则存入。
		Verify dbVerify = verifyDao.queryVerifyLastlyByMid(verify.getMid());
		if (dbVerify == null) {
			if (verifyDao.addVerify(verify) > 0) {
				long id = verifyDao.getCurrVal();
				verify.setId(Integer.parseInt(id+""));
				//添加缓存
				VerifyCache.add(verify);
				return true;
			}
		} else if (dbVerify != null
				&& dbVerify.getState() != Const.STATE_SUCCESS) {
			if (verifyDao.addVerify(verify) > 0) {
				long id = verifyDao.getCurrVal();
				verify.setId(Integer.parseInt(id+""));
				VerifyCache.add(verify);
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean deleteVerify(Integer id) {
		if (verifyDao.deleteVerify(id) > 0) {
			Verify v = verifyDao.queryVerifyById(id);
			VerifyCache.delete(v.getMid());
			return true;
		}
		return false;
	}

	@Override
	public boolean updateVerifyState(Verify verify) {
		Merchant merchant = new Merchant();
		merchant.setId(verify.getMid());
		merchant.setName(verify.getName());
		merchant.setStoreName(verify.getStoreName());
		merchant.setPwd(verify.getPwd());
		merchant.setIdcard(verify.getIdcard());
		merchant.setImg(verify.getImg());
		merchant.setAddress(verify.getAddress());
		merchant.setState(verify.getState());
		merchant.setBlock(verify.getBlock());
		// 如果成功，同步进MUser表
		if (verify.getState().equals(Const.STATE_SUCCESS)) {
			if (merchantDao.addMerchant(merchant) > 0) {
				// 更新缓存
				MerchantDto dto = merchantService.convert(merchant);
				MerchantCache.add(dto);
			}
		}
		// 更新state
		if (verifyDao.updateVerify(verify) > 0) {
			verifyDao.modifyState(verify.getMid(), Const.STATE_FAIL);
			//更新缓存
			VerifyCache.delete(verify.getMid());
			return true;
		}
		return false;
	}

	/**
	 * 修改黑白名单（同步Verify 和 m_user）
	 */
	@Override
	public boolean modifyBlock(String mid, Integer block) {
		Merchant merchant = merchantDao.queryMerchantById(mid);
		if (merchant != null) {
			merchant.setBlock(block);
			merchantDao.updateMerchant(merchant);
			// 更新缓存
			if (Const.BLOCK_BLACK.equals(block)) {
				MerchantCache.delete(merchant.getId());
			} else if (Const.BLOCK_WHITE.equals(block)) {
				MerchantDto dto = merchantService.convert(merchant);
				MerchantCache.update(dto);
			}
			if (verifyDao.modifyBlock(mid, block) > 0) {
				Verify verify = verifyDao.queryVerifyLastlyByMid(mid);
				//更新缓存
				VerifyCache.update(verify);
				// 进行JMS发送TODO
				// JMSUtil.send(ServerConfig.JMS_PRODUCER_QUEUE,
				// JsonUtil.getJsonFromObject(verify));
				return true;
			}
		}
		return false;
	}

	@Override
	public List<Verify> queryAllVerify(Integer state) {
		List<Verify> vs = verifyDao.queryAllVerifyLastly(state);
		for (Verify v : vs) {
			v.setPwd(null);
		}
		return vs;
	}

	@Override
	public Verify queryVerifyLastlyByMid(String mid) {
		return verifyDao.queryVerifyLastlyByMid(mid);
	}

	@Override
	public Verify queryVerifyById(Integer id) {
		return verifyDao.queryVerifyById(id);
	}
	
	@Override
	public int countNewVerify(long time) {
		int counter = 0;
		Map<String, Verify> map = VerifyCache.getCache();
		Set<String> keys = map.keySet();
		for(String key : keys){
			if(map.get(key).getSubmitTime().longValue() > time){
				counter++;
			}
		}
		return counter;
	}

}
